import { ref, computed } from 'vue'
import store from '../store'
import { fetchManager, fetchMangerVips } from '../axioser'

export function useManager() {
  // 绑定会员信息
  const bind_vip = {
    name: '',
    uid: '',
    _wx: '',
  }
  // 管理员信息
  const managerOptions = {
    uid: '',
    name: '',
    privilege_level: undefined,
    state: 0,
    bind_vip,
  }
  // 当前管理员下线
  const currentManagerVips = ref({
    list: [],
    total: 0,
    pageSize: 10,
    currentIndex: 1,
    over: false,
  })
  // 返回当前管理员信息
  const currentManager = computed(() => {
    if (store.state.manager.currentManager.uid.length === 0)
      store.dispatch('refreshManager')
    return store.state.manager.currentManager
  })
  // 获取当前管理员下线
  const getManagerVips = async (currentIndex, uid, keyword, _result) => {
    try {
      const {
        data: { data },
      } = await fetchMangerVips({ currentIndex, uid, keyword, _result })
      currentManagerVips.value.list.push(...data.list)
      currentManagerVips.value.currentIndex = data.currentIndex
      currentManagerVips.value.over =
        currentManagerVips.value.pageSize > data.list.length
      currentManagerVips.value.total = data.total
    } catch (error) {
      console.error(error)
    }
  }
  // 获取管理员信息
  const getManagerInfo = async (_uid = undefined) => {
    try {
      const {
        data: {
          data: { uid, name, privilege_level, state, bind_vip },
        },
      } = await fetchManager({ uid: _uid })
      return {
        uid,
        name,
        privilege_level,
        state,
        bind_vip,
      }
    } catch (error) {
      console.error(error)
    }
  }

  return {
    bind_vip,
    managerOptions,
    getManagerInfo,
    currentManager,
    getManagerVips,
    currentManagerVips,
  }
}
